package javacompiler.codegenerator;

import java.util.ArrayDeque;

public class RegisterFactory {
	private ArrayDeque<Register> pool;
	private int poolSize;
	
	public RegisterFactory(int poolSize) {
		this.poolSize = poolSize;
		pool = new ArrayDeque<Register>(poolSize);
		
		for (int i = 0; i < poolSize; i++) {
			pool.add(new Register("R" + i));
		}
	}
	
	public void free(Register register) {
		if (pool.size() <= poolSize) {
			pool.add(register);
		}
	}
	
	public Register getFreeRegister() {
		return pool.remove();
	}
}
